<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">
          <a-col :md="2" :sm="4">
            <a-form-item>
              <a-date-picker placeholder="开始日期" @change="onChangeStartTime"/>
            </a-form-item>
          </a-col>
          <a-col :md="2" :sm="4">
            <a-form-item>
              <a-date-picker placeholder="结束日期" @change="onChangeEndTime"/>
            </a-form-item>

          </a-col>

          <a-col :md="4" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x: 1000}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading" @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <config-park-charge-modal ref="modalForm"></config-park-charge-modal>
  </a-card>
</template>

<script>
import ConfigParkChargeModal from './modules/ConfigParkChargeModalDetail'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {deleteAction, getAction, downFile} from '@/api/manage'
import {filterObj} from '@/utils/util';

export default {
  name: "ConfigParkChargeList",
  mixins: [JeecgListMixin],
  components: {
    ConfigParkChargeModal,
  },
  data() {
    return {
      description: '停车场计费规则信息表管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 45,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '停车场编码',
          align: "center",
          width: 107,
          dataIndex: 'parkCode'
        },
        {
          title: '停车场名称',
          align: "center",
          width: 187,
          dataIndex: 'parkName'
        },
        {
          title: '车辆类型',
          align: "center",
          width: 103,
          dataIndex: 'carTypeId'
        },
        {
          title: '计费规则类型',
          align: "center",
          width: 115,
          dataIndex: 'ratesType',
          customRender: (v) => ('0' === v ? '按时间增量计费' : '1' === v ? '按白天黑夜计费' : ''),
        },
        {
          title: '免费时间状态',
          align: "center",
          width: 115,
          dataIndex: 'freeTimeStatus',
          customRender: (v) => ('N' === v ? '无免费时间' : 'Y' === v ? '有免费时间' : '')
        },
        {
          title: '创建人', width: 109,
          align: "center",
          dataIndex: 'createUser'
        },
        {
          title: '修改时间', width: 153,
          align: "center",
          dataIndex: 'updateTime'
        },
        {
          title: '创建时间', width: 153,
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          width: 100,
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        list: "/parking/configParkCharge/Translist",
        delete: "/parking/configParkCharge/delete",
        deleteBatch: "/parking/configParkCharge/deleteBatch",
        exportXlsUrl: "parking/configParkCharge/exportXls",
        importExcelUrl: "parking/configParkCharge/importExcel",
        CarNameUrl: "/ConfigCarType/configCarType/queryById"
      },
      queryParam: {
        "createTime_begin": "",
        "createTime_end": "",
        "type": "-1",

      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  created() {

    this.keyupEnter()
  },
  methods: {

    onChangeStartTime(date, dateString) {
      this.queryParam.createTime_begin = dateString;
    },

    onChangeEndTime(date, dateString) {
      this.queryParam.createTime_end = dateString;
    },

    loadData(arg) {
      let that = this
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }

      let params = this.getQueryParams();//查询条件
      if(this.parkCode===""){
        params.parkCode=null;
      }
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          that.dataSource = res.result.records;
          that.ipagination.total = res.result.total;
        }
        if (res.code === 510) {
          that.$message.warning(res.message)
        }
        this.loading = false
      })

    },

    searchQuery() {
      let startTime = this.queryParam.createTime_begin;
      let endTime = this.queryParam.createTime_end;

      if (startTime !== "" && endTime !== "") {
        if (startTime > endTime) {
          this.$message.error("开始时间不能大于结束时间");
          return
        }
      }

      if (startTime === "" && endTime !== "") {
        this.$message.error("请选择开始时间");
        return
      }

      if (startTime !== "" && endTime === "") {
        this.$message.error("请选择结束时间");
        return
      }

      this.loadData(1);
    },

    getQueryParams() {

      var param = Object.assign({}, this.isorter, this.queryParam);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      param.parkCode= this.parkCode;
      if (this.queryParam.createTime_begin !== "" && this.queryParam.createTime_end !== "") {
        param.craeteTime_begin = this.queryParam.createTime_begin + " 00:00:00";
        param.createTime_end = this.queryParam.createTime_end + " 23:59:59";
      }

      // delete param.createTimeRange; // 时间参数不传递后台
      return filterObj(param);
    },

    handleDelete: function (invateuserdetailId) {
      if (!this.url.delete) {
        this.$message.error("请设置url.delete属性!")
        return
      }
      var that = this;
      deleteAction(that.url.delete, {id: invateuserdetailId}).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.loadData();
        } else {
          that.$message.warning(res.message);
        }
      });
    },

    keyupEnter() {
      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1);
        }
      }
    },
  }
}
</script>
<style scoped>

</style>
